package com.example.fragmentdemo.adapter;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;

import java.util.ArrayList;
import java.util.List;

public class FragmentTitleAdapter extends FragmentStatePagerAdapter {

    public List<Fragment> fragmentList=new ArrayList<>();

    public List<String> titleList=new ArrayList<>();

    public FragmentTitleAdapter(@NonNull FragmentManager fm, List<Fragment> fragmentList,
                                List<String> titleList) {
        super(fm);
        this.fragmentList.addAll(fragmentList);
        this.titleList.addAll(titleList);
    }
    public void addFragment(Fragment fragment,String title){
        fragmentList.add(fragment);
        titleList.add(title);
    }
    @NonNull
    @Override
    public Fragment getItem(int position) {
        return fragmentList==null?null:fragmentList.get(position);
    }

    @Override
    public int getCount() {
        return fragmentList==null?0: fragmentList.size();
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return titleList==null?"":titleList.get(position);
    }
}
